package mod.luckymod.blocks;

import mod.luckymod.fluids.AllFluids;
import mod.luckymod.util.LaunchEntity;
import mod.luckymod.util.ScheduleConditionalTask;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.core.particles.ParticleTypes;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.sounds.SoundSource;
import net.minecraft.util.RandomSource;
import net.minecraft.world.entity.item.FallingBlockEntity;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.LevelAccessor;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.FallingBlock;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.material.FluidState;
import net.minecraft.tags.FluidTags;
import org.jetbrains.annotations.NotNull;

public class SodiumBlock extends FallingBlock {
    ScheduleConditionalTask task = new ScheduleConditionalTask();
    boolean Splitting;
    public SodiumBlock(Properties properties) {
        super(properties);
        this.Splitting = true;
    }

    // 检测是否在水中（类似混凝土粉末的逻辑）
    private boolean isInWater(Level level, BlockPos pos) {
        FluidState fluidState = level.getFluidState(pos);
        if (fluidState.is(FluidTags.WATER) && !fluidState.isEmpty()) {
            return true;
        }

        // 检查六个方向是否接触水
        BlockPos.MutableBlockPos mutablePos = new BlockPos.MutableBlockPos();
        for (Direction direction : Direction.values()) {
            mutablePos.setWithOffset(pos, direction);
            FluidState adjacentFluid = level.getFluidState(mutablePos);
            if (adjacentFluid.is(FluidTags.WATER) && !adjacentFluid.isEmpty()) {
                return true;
            }
        }

        return false;
    }
    // 钠块嘶嘶声开始操作
    private void sodiumStartAction(Level level, BlockPos pos) {
        level.playSound(null, pos, SoundEvents.FIRE_EXTINGUISH, SoundSource.BLOCKS, 0.7F, 1.2F);
        if(level instanceof ServerLevel serverLevel) {
            serverLevel.sendParticles(ParticleTypes.CAMPFIRE_SIGNAL_SMOKE,
                    pos.getX() + 0.5, pos.getY() + 0.5, pos.getZ() + 0.5,
                    10, 0.5, 0.5, 0.5, 0.05);
        }
    }

    // 钠块爆炸结束操作
    private void sodiumEndAction(Level level, BlockPos pos) {
        // 检查钠块是否还存在
        if (level.getBlockState(pos).getBlock() == this) {
            triggerExplosion(level, pos);
        }
    }

    @Override
    public void tick(@NotNull BlockState state, @NotNull ServerLevel level, BlockPos pos, RandomSource random){
        super.tick(state, level, pos, random);
        task.scheduleConditionalAction(isInWater(level, pos), level, pos, 4,
                this::sodiumStartAction, this::sodiumEndAction);
    }

    @Override
    public void onPlace(BlockState state, Level level, BlockPos pos, BlockState oldState, boolean isMoving) {
        super.onPlace(state, level, pos, oldState, isMoving);
        task.scheduleConditionalAction(isInWater(level, pos), level, pos, 4,
                this::sodiumStartAction, this::sodiumEndAction);
    }

    @Override
    public void onLand(Level level, BlockPos pos, BlockState state, BlockState replacedState, FallingBlockEntity fallingBlock) {
        task.scheduleConditionalAction(isInWater(level, pos), level, pos, 4,
                this::sodiumStartAction, this::sodiumEndAction);
    }

    @Override
    public BlockState updateShape(BlockState state, Direction direction, BlockState neighborState, LevelAccessor level, BlockPos currentPos, BlockPos neighborPos) {
        return super.updateShape(state, direction, neighborState, level, currentPos, neighborPos);
    }

    // 执行爆炸逻辑
    public void triggerExplosion(Level level, BlockPos pos) {
        if (!(level instanceof ServerLevel)) return;

        ServerLevel serverLevel = (ServerLevel) level;

        // 移除钠方块
        serverLevel.removeBlock(pos, false);

        double centerX = pos.getX() + 0.5;
        double centerY = pos.getY() + 0.5;
        double centerZ = pos.getZ() + 0.5;

        // 创建爆炸（与TNT相同的威力4.0F）
        serverLevel.explode(
                null,
                centerX, centerY, centerZ,
                4.0F,
                Level.ExplosionInteraction.BLOCK
        );

        // 播放爆炸音效
        serverLevel.playSound(null, pos, SoundEvents.GENERIC_EXPLODE, SoundSource.BLOCKS, 1.0F, 0.9F + serverLevel.random.nextFloat() * 0.2F);

        // 生成爆炸粒子
        generateExplosionParticles(serverLevel, pos);
        if(Splitting) {
            LaunchEntity.LanuchEntity(pos, serverLevel, 3);
        }
        FluidState fluidBlockState = AllFluids.NAOH_Entry.get().getSource(true);
        serverLevel.setBlock(pos, fluidBlockState.createLegacyBlock(), Block.UPDATE_ALL);
    }

    // 生成爆炸粒子效果
    void generateExplosionParticles(ServerLevel level, BlockPos pos) {
        RandomSource random = level.random;
        for (int i = 0; i < 20; ++i) {
            double xSpeed = (random.nextDouble() - 0.5) * 2.0;
            double ySpeed = (random.nextDouble() - 0.5) * 2.0;
            double zSpeed = (random.nextDouble() - 0.5) * 2.0;

            level.sendParticles(ParticleTypes.POOF,
                    pos.getX() + 0.5, pos.getY() + 0.5, pos.getZ() + 0.5,
                    100, xSpeed, ySpeed, zSpeed, 0.1);
        }
        // 爆炸烟雾
        level.sendParticles(ParticleTypes.CAMPFIRE_SIGNAL_SMOKE,
                pos.getX() + 0.5, pos.getY() + 0.5, pos.getZ() + 0.5,
                10, 0.5, 0.5, 0.5, 0.05);
    }
    }